package com.intelligence.tio.server.handler;

import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.json.Json;

import com.intelligence.tio.common.Command;
import com.intelligence.tio.common.JsonPacket;
import com.intelligence.tio.common.TioSessionContext;
import com.intelligence.tio.common.intf.AbsTioBsHandler;
import com.intelligence.tio.common.packages.AuthReqBody;
import com.intelligence.tio.common.packages.AuthRespBody;

/**
 * 
* @Title: AuthReqHandler.java 
* @Package com.intelligence.tio.server.handler 
* @Description: 用户授权消息处理器 
* @Company: 南京因坦利软件有限公司
* @author 郭浩柱  
* @date 2017年5月18日 下午3:35:54 
* @version V1.0
 */
public class AuthReqHandler extends AbsTioBsHandler<AuthReqBody>
{
	private static Logger log = LoggerFactory.getLogger(AuthReqHandler.class);

	/** 
	 * @return
	 * @author: tanyaowu
	 */
	@Override
	public Class<AuthReqBody> bodyClass()
	{
		return AuthReqBody.class;
	}

	java.util.concurrent.atomic.AtomicLong tokenSeq = new AtomicLong();

	private String newToken()
	{
		return System.currentTimeMillis() + "_" + tokenSeq.incrementAndGet();
	}

	/** 
	 * @param packet
	 * @param bsBody
	 * @param channelContext
	 * @return
	 * @throws Exception
	 * @author: tanyaowu
	 */
	@Override
	public Object handler(JsonPacket packet, AuthReqBody bsBody, ChannelContext<TioSessionContext, JsonPacket, Object> channelContext) throws Exception
	{
		log.info("收到授权请求消息:{}", Json.toJson(bsBody));
		AuthRespBody authRespBody = new AuthRespBody();
		String token = bsBody.getToken();
		authRespBody.setToken(token);
		String userId = bsBody.getUserId();
		String userName = bsBody.getUserName();
		String userPhone = bsBody.getUserPhone();
		String groupId = bsBody.getGroupId();
		Integer roleId = bsBody.getRoleId();

//		//判断用户是否已经进入班级
//		Aio.getChannelContextByUserid(con, userid)
//		
//		
//		channelContext.


		try {


			//绑定用户
			Aio.bindUser(channelContext, userId);
			log.info("通道上下文Id"+channelContext.getId());


			//绑定用户进入聊天室组
			Aio.bindGroup(channelContext, groupId);
			authRespBody.setCode(1);
			authRespBody.setMsg("用户加入聊天室成功！");

			ChannelContext<TioSessionContext, JsonPacket, Object> channelContextByUserid = Aio.getChannelContextByUserid(channelContext.getGroupContext(), userId);



			log.info("获取第二个"+channelContextByUserid.getId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			authRespBody.setCode(0);
			authRespBody.setMsg("用户加入聊天室失败！");
		}
		//设置私聊响应码
		authRespBody.setCommand(Command.COMMAND_AUTH_RESP.getValue());
		TioSessionContext tioSessionContext = channelContext.getSessionContext();
		tioSessionContext.setUserId(userId);
		tioSessionContext.setUserName(userName);
		tioSessionContext.setUserPhone(userPhone);
		tioSessionContext.setToken(token);
		tioSessionContext.setRoleId(roleId);
		tioSessionContext.setGroupId(groupId);
		
		JsonPacket respPacket = new JsonPacket();
		respPacket.setBody(Json.toJson(authRespBody).getBytes(JsonPacket.CHARSET));
		Aio.send(channelContext, respPacket);

		return null;
	}
}
